﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KSAWPFDemo
{
    /// <summary>
    /// Interaction logic for Home.xaml
    /// </summary>
    public partial class Home : UserControl
    {
        System.Timers.Timer timer;

        public Home()
        {
            InitializeComponent();
            MainGrid.Opacity = 0;
            this.Loaded += Home_Loaded;
            timer = new System.Timers.Timer();
            timer.Interval = 1;
            timer.Elapsed += timer_Elapsed;
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            Time.Dispatcher.BeginInvoke(new Action(delegate()
            {
              Time.Content = DateTime.Now.ToString();
              GetStatus();
            }));
            
            
        }


        void Home_Loaded(object sender, RoutedEventArgs e)
        {
            timer.Start();
   
            DoubleAnimation fadeIn = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(550)));
            foreach (UIElement child in MainGrid.Children)
            {
                child.Opacity = 0;
            }
            MainGrid.BeginAnimation(UIElement.OpacityProperty, fadeIn);
            for (int i = 0; i < MainGrid.Children.Count;i++ )
            {
                UIElement elem = MainGrid.Children[i];
                fadeIn.BeginTime = TimeSpan.FromMilliseconds((i * 160) + 300);
                elem.BeginAnimation(UIElement.OpacityProperty, fadeIn);
            }

        }

        private void GetStatus()
        {
            string perfData = Performance.PerformanceManager.GetAllPerformanceData();
            Status.Content = perfData;
            
        }

        /// <summary>
        /// Courtesy of Josh Blake (again) http://nui.joshland.org/2010/04/why-wont-wpf-controls-work-with-touch.html
        /// Highly recommended blog!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_TouchDown(object sender, TouchEventArgs e)
        {
            FrameworkElement button = sender as FrameworkElement;
            if (button == null)
                return;

            button.CaptureTouch(e.TouchDevice);

            e.Handled = true;

        }

        private void button_TouchUp(object sender, TouchEventArgs e)
        {
            FrameworkElement button = sender as FrameworkElement;
            if (button == null)
                return;

            button.ReleaseTouchCapture(e.TouchDevice);

            e.Handled = true;
        }

        private void Button_PreviewTouchDown(object sender, TouchEventArgs e)
        {
            FrameworkElement button = sender as FrameworkElement;
            if (button == null)
                return;
            ((Button)button).Background = Brushes.Red; // highlight the button for feedback
            button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
            e.Handled = true;
            ((Button)button).Background = Brushes.Gray; // highlight the button for feedback
        }

        private void Button_PreviewTouchUp_1(object sender, TouchEventArgs e)
        {
            FrameworkElement button = sender as FrameworkElement;
            if (button == null)
                return;

            button.ReleaseTouchCapture(e.TouchDevice);

            e.Handled = true;
        }

        private void SelectChild(object sender, RoutedEventArgs e)
        {
            MainWindow main = MainWindow.GetWindow(this) as MainWindow;
            Button button = sender as Button;
            if (button != null)
            {
                string selection = button.Tag as string;
                main.SelectChild(selection);
            }
        }
    }
}
